"""
飞机大战
"""
import time
import const

import pygame
from pygame.locals import *


class Plane(object):
    """飞机父类"""

    def __init__(self, x, y, image, screen):
        """初始化"""
        self.x = x
        self.y = y
        self.screen = screen
        self.image = pygame.image.load(image)

    def move_left(self):
        """左移动"""
        self.x -= const.MOVE_STEP

    def move_right(self):
        """右移动"""
        self.x += const.MOVE_STEP

    def move_up(self):
        """上移动"""
        self.y -= const.MOVE_STEP

    def move_down(self):
        """右移动"""
        self.y += const.MOVE_STEP


class Hero(Plane):
    """
    英雄飞机
    """

    def __init__(self, screen):
        """初始化"""
        super().__init__(190, 605, const.IMG_HERO, screen)

    def display(self):
        """显示到屏幕上"""
        self.screen.blit(self.image, (self.x, self.y))


def key_control(plane):
    """键盘监听器"""
    # 获取事件，比如按键等
    for event in pygame.event.get():
        # 判断是否点击了退出按钮
        if event.type == QUIT:
            print("退出了游戏...")
            exit()
        # 判断是否按下了键
        elif event.type == KEYDOWN:
            # 判断是否按下了a或者left
            if event.key == K_a or event.key == K_LEFT:
                print("left")
                plane.move_left()

            # 判断是否按下了d或者right
            elif event.key == K_d or event.key == K_RIGHT:
                print("right")
                plane.move_right()

            # 判断是否按下了a或者left
            if event.key == K_w or event.key == K_UP:
                print("up")
                plane.move_up()

            # 判断是否按下了d或者right
            elif event.key == K_s or event.key == K_DOWN:
                print("down")
                plane.move_down()

            # 判断是否按下了空格
            elif event.key == K_SPACE:
                print("space")


def main():
    """主函数"""
    # 1. 创建窗口
    screen = pygame.display.set_mode((460, 680), 0, 32)

    # 2. 创建背景
    background = pygame.image.load(const.IMG_BG)

    # 3. 创建一个飞机
    hero = Hero(screen)

    # 4. 刷新屏幕
    while True:
        screen.blit(background, (0, 0))

        hero.display()

        pygame.display.update()

        key_control(hero)

        time.sleep(0.01)


if __name__ == '__main__':
    main()
